package top.saodisheng.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import top.saodisheng.dao.UserDao;
import top.saodisheng.pojo.User;
import top.saodisheng.util.Page4Navigator;

import java.util.List;

/**
 * Description:
 *
 * @author 扫地生_saodisheng
 */
@Service
public class UserService {
    @Autowired
    UserDao userDao;

    // 获取用户列表
    public List<User> list() {
        return userDao.findAll();
    }
    // 获取用户列表，按编号升序
    public Page4Navigator<User> list(int start, int size, int navigatePage) {
        Page pageFromJPA = userDao.findAll(PageRequest.of(start, size,
                Sort.by(Sort.Direction.DESC, "id")));
        return new Page4Navigator<>(pageFromJPA, navigatePage);
    }

    // 删除用户
    public void delete(int id) {
        userDao.deleteById(id);
    }

    // 更新用户
    public void update(User bean) {
        userDao.save(bean);
    }

    // 通过用户名获取用户
    public User getByName(String name) {
        return userDao.findByName(name);
    }

    public User get(int id) {
        return userDao.getOne(id);
    }

    // 添加用户
    public void add(User user) {
        userDao.save(user);
    }

    // 获取数据库表的记录数
    public int count() {
        return (int)userDao.count();
    }
}
